<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/**
 * Basketball extension for Joomleague
 * @version $Id: BasketballProject.php 103 2008-03-05 08:19:47Z julien.vonthron $
 * @package JlgBasketballExtension
 * @copyright Copyright (C) 2007 Julien Vonthron. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * JlgBasketballExtension is free software and parts of it may contain or be derived from the
 * GNU General Public License or other free or open source software licenses.
 * 
 * contact: julien.vonthron[at]gmail.com 
 *   
 * Joomleague is a League Management and Prediction Game for Joomla!
 * Homepage: http://www.joomleague.de 
 *  
 */

class BasketballProject extends JoomLeague
{
	/**
	 * gives games played/came in/came out stats for player
	 *
	 * @param int $player_id
	 * @param int $project_id
	 * @param int $team_id
	 * @return Object (members: played, subst, sub_out)
	 */
  function getStartInOutStats($player_id, $project_id, $team_id) 
  {
    global $database;
    $query = "SELECT count(mp.id) as played, sum(mp.came_in) as subst, sum(mp.out = 1) AS sub_out
              FROM #__joomleague_match_players AS mp 
              INNER JOIN #__joomleague_matches AS m ON mp.match_id = m.match_id 
              WHERE mp.player_id = '$player_id' 
                AND m.project_id = '$project_id'
                AND mp.team_id = '$team_id'";
    $database->setQuery($query);
    $result = NULL;
    $database->loadObject($result);
    return $result;
  }
  
  /**
   * return stat for specified player id and eventtype id
   *
   * @param int $pid player id
   * @param int $evid eventtype id
   * @param int $team_id 
   * @return unknown
   */
  function getPlayerEvents($pid, $evid, $team_id) 
  {
    return BasketballProject::getPlayerEventStat($pid, $evid, $this->id, $team_id);
  }

  /**
   * return the sum of the total of event_sum for event $event_id divided by the
   * total of event_sum of $event_total_id   
   *
   * @param int $event_id
   * @param int $event_total_id
   * @param int $project_id
   * @param int $player_id
   * @param int $team_id
   * @return float
   */
  function getAverageEventStat( $event_id, $event_total_id, $project_id, $player_id, $team_id )
  {
    global $database;
    // sub request for event
    $sub_event = 'SELECT SUM(me.event_sum) AS total_event
  			                  FROM #__joomleague_match_events AS me
  			                  INNER JOIN #__joomleague_match_players AS mp USING (match_id, player_id)
  			                  WHERE me.player_id = '.$player_id
  			                 .' AND me.event_type_id = '.$event_id 
  			                 .' AND me.project_id = '.$project_id
  			                 .' AND mp.team_id = '.$team_id;
  	$sub_event_total = 'SELECT SUM(me.event_sum) AS total_event_sum
  			                  FROM #__joomleague_match_events AS me
  			                  INNER JOIN #__joomleague_match_players AS mp USING (match_id, player_id)
  			                  WHERE me.player_id = '.$player_id
  			                 .' AND me.event_type_id = '.$event_total_id 
  			                 .' AND me.project_id = '.$project_id
  			                 .' AND mp.team_id = '.$team_id;
  			                    
  	$query = 'SELECT total_event, total_event_sum
                  FROM ('.$sub_event.') AS se, ('.$sub_event_total.') AS se_sum
                  WHERE total_event_sum > 0';
    $database->setQuery($query);
    if ($result = $database->loadRow()) {
      return ($result[0]/$result[1]);
    }
  }
  
  /**
   * return the average per game for event $event_id
   *
   * @param int $event_id
   * @param int $project_id
   * @param int $player_id
   * @param int $team_id
   * @return float
   */
  function getGameAverageEventStat( $event_id, $project_id, $player_id, $team_id )
  {
    global $database;
    // sub request for event
    $sub_event = 'SELECT SUM(me.event_sum) AS total_event
  			                  FROM #__joomleague_match_events AS me
  			                  INNER JOIN #__joomleague_match_players AS mp USING (match_id, player_id)
  			                  WHERE me.player_id = '.$player_id
  			                 .' AND me.event_type_id = '.$event_id 
  			                 .' AND me.project_id = '.$project_id
  			                 .' AND mp.team_id = '.$team_id;
  	// sub request for games played
	  $sub_games_played = "SELECT COUNT(*) AS games_played
	                        FROM #__joomleague_match_players AS mp
	                        INNER JOIN #__joomleague_matches AS m USING (match_id)
	                        WHERE mp.player_id = $player_id
	                          AND mp.team_id=$team_id
	                          AND m.project_id = $project_id		                          
	                        GROUP BY mp.player_id";
  			                    
  	$query = 'SELECT total_event, games_played
                  FROM ('.$sub_event.') AS se, ('.$sub_games_played.') AS gp
                  WHERE games_played > 0';
    $database->setQuery($query);
    if ($result = $database->loadRow()) {
      return ($result[0]/$result[1]);
    }
  }
  
  /**
   * return stat for specified player id and eventtype id
   *
   * @param int $pid player id
   * @param int $evid eventtype id
   * @param int $project_id 
   * @param int $team_id 
   * @return unknown
   */
  function getPlayerEventStat($pid, $evid, $project_id, $team_id) 
  {
    global $database, $overall_config;
    // manual events
    $event_points       = $overall_config['event_points'];
    $event_rebounds     = $overall_config['event_rebounds'];
    $event_assists      = $overall_config['event_assists'];
    $event_steals       = $overall_config['event_steals'];
    $event_blocks       = $overall_config['event_blocks'];
    $event_3pt_made     = $overall_config['event_3pt_made'];
    $event_3pt_attempts = $overall_config['event_3pt_attempts'];
    $event_fg_made      = $overall_config['event_fg_made'];
    $event_fg_attempts  = $overall_config['event_fg_attempts'];
    $event_ft_made      = $overall_config['event_ft_made'];
    $event_ft_attempts  = $overall_config['event_ft_attempts'];
    
    // calculated events
    $event_ppg       = $overall_config['event_ppg'];
    $event_rpg       = $overall_config['event_rpg'];
    $event_apg       = $overall_config['event_apg'];
    $event_spg       = $overall_config['event_spg'];
    $event_bpg       = $overall_config['event_bpg'];
    $event_3pt_per   = $overall_config['event_3pt_per'];
    $event_fg_per    = $overall_config['event_fg_per'];
    $event_ft_per    = $overall_config['event_ft_per'];
    
    switch ($evid) {
      // first, all the 'per game' stats
      case $event_ppg:
        return BasketballProject::getGameAverageEventStat( $event_points, 
                                                $project_id,
                                                $pid,
                                                $team_id );
      case $event_rpg:
        return BasketballProject::getGameAverageEventStat( $event_rebounds, 
                                                $project_id,
                                                $pid,
                                                $team_id );
      case $event_apg:
        return BasketballProject::getGameAverageEventStat( $event_assists, 
                                                $project_id,
                                                $pid,
                                                $team_id );
      case $event_spg:
        return BasketballProject::getGameAverageEventStat( $event_steals, 
                                                $project_id,
                                                $pid,
                                                $team_id );
      case $event_bpg:
        return BasketballProject::getGameAverageEventStat( $event_blocks, 
                                                $project_id,
                                                $pid,
                                                $team_id );
      case $event_3pt_per:
        return BasketballProject::getAverageEventStat( $event_3pt_made, 
                                                $event_3pt_attempts,
                                                $project_id,
                                                $pid,
                                                $team_id );
      case $event_fg_per:
        return BasketballProject::getAverageEventStat( $event_fg_made, 
                                                $event_fg_attempts,
                                                $project_id,
                                                $pid,
                                                $team_id );
      case $event_ft_per:
        return BasketballProject::getAverageEventStat( $event_ft_made, 
                                                $event_ft_attempts,
                                                $project_id,
                                                $pid,
                                                $team_id );
      default:
        // just the sum
        $query = "SELECT SUM(me.event_sum)
                  FROM #__joomleague_match_events AS me
                  INNER JOIN #__joomleague_match_players AS mp USING (match_id, player_id)
                  WHERE me.player_id = $pid
                    AND me.event_type_id = $evid 
                    AND me.project_id = ".$project_id."
                    AND mp.team_id=$team_id";
        $database->setQuery($query);
        return $database->loadResult();
        break;
    }
    return $result;
  }
}	
?>
